<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="最多吐槽120字" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" v-on:click="postComment">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in commentList" :key="item.id">
                <div class="cmt-title">
                    第{{i+1}}楼 &nbsp;&nbsp;&nbsp; {{item.user_name}} &nbsp;&nbsp; 发表时间：{{item.add_time | dateFormat}}
                </div>
                <div class="cmt-body">
                    {{!item.content || item.content === 'undefined' ? '此用户很懒' : item.content}}
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain v-on:click="getMore">加载更多</mt-button>
    </div>
</template>
<script>
    import { Toast } from 'mint-ui';
    export default {
        data() {
            return {
                commentList: [],
                pageindex: 1,
                msg : ''
            }
        },
        created() {
            this.getComments();
        },
        methods: {
            getComments() {
                this.$http.get('http://vue.studyit.io/api/getcomments/'+this.id+'?pageindex='+this.pageindex).then(result => {
                    //console.log(result);
                    if( result.body.status === 0 ){
                        this.commentList = this.commentList.concat(result.body.message);
                        // console.log(this.commentList)
                    }else{
                        Toast('获取失败');
                    }
                });
            },
            getMore() {
                this.pageindex++;
                this.getComments();
            },
            postComment() {
                if( this.msg.trim().length === 0 ){
                    //如果为空
                    Toast('评论内容不能为空');
                }else{
                    // post
                    // 参数1， 请求的url
                    // 参数2，请求的参数
                    this.$http.post('http://vue.studyit.io/api/postcomment/'+this.$route.params.id,{
                        content: this.msg
                    }).then(result => {
                        if( result.body.status === 0 ) {
                            //请求成功了，往commentList中添加一条
                            var cmt = {add_time: new Date(), content: this.msg.trim(), user_name: '匿名用户'};
                            this.commentList.unshift(cmt);
                            this.msg = '';
                            Toast('提交成功');
                        }
                    });
                }
            }
        },
        props:['id']
    }
</script>
<style lang="less" scoped>
.cmt-container{
    h3{
        font-size: 18px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
    }
    .cmt-list{
        margin:5px 0;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                line-height: 30px;
                background: #eee;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
    }
    
}
</style>
